package com.fqgj.sentry.policy.server;

import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.policy.client.ColumnService;
import com.fqgj.sentry.policy.dao.PcColumnDao;
import com.fqgj.sentry.policy.dao.PcFunctionColumnDao;
import com.fqgj.sentry.policy.dao.PcFunctionContextDao;
import com.fqgj.sentry.policy.dao.PcRuleColumnDao;
import com.fqgj.sentry.policy.dao.param.ColumnMap;
import com.fqgj.sentry.policy.domain.column.Column;
import com.fqgj.sentry.policy.domain.column.ColumnQueryInfo;
import com.fqgj.sentry.policy.entity.PcColumnEntity;
import com.fqgj.sentry.policy.entity.PcFunctionColumnEntity;
import com.fqgj.sentry.policy.entity.PcRuleColumnEntity;
import com.fqgj.sentry.policy.enums.ColumnStatusEnum;
import com.fqgj.sentry.policy.enums.ColumnTypeEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fqgj.common.utils.CollectionUtils;

import java.util.List;

/**
 * Created by zwh @yuntu-inc.com
 *
 * @desc
 * @create 2017/7/29 0029 下午 2:53
 */
@Service
public class ColumnServiceImpl implements ColumnService {

    @Autowired
    private PcColumnDao pcColumnDao;
    @Autowired
    private PcRuleColumnDao pcRuleColumnDao;
    @Autowired
    private PcFunctionColumnDao functionColumnDao;
    @Autowired
    private PcFunctionContextDao functionContextDao;

    @Override
    public PcColumnEntity saveColumn(PcColumnEntity pcColumnEntity) {
        if (StringUtils.isEmpty(pcColumnEntity.getColumnCode())) {
            throw new BizException(ErrorCodeEnums.column_code_is_not_exist_error);
        }
        //该商户columnCode不能重复
        PcColumnEntity columnEntity = pcColumnDao.selectByMerchantIdAndColumnCode(pcColumnEntity.getMerchantId(), pcColumnEntity.getColumnCode());
        if (null != columnEntity) {
            throw new BizException(ErrorCodeEnums.column_code_is_exist_error);
        }
        return pcColumnDao.insert(pcColumnEntity);
    }

    @Override
    public Boolean updateColumn(PcColumnEntity pcColumnEntity) {
        return pcColumnDao.updateByPrimaryKey(pcColumnEntity) > 0;
    }

    /**
     * 更新字段
     *
     * @param column
     * @return
     */
    @Override
    public Integer updateColumn(Column column) {
        //查询
        PcColumnEntity pcColumnEntity = selectColumn(column.getId());
//        if (pcColumnEntity.getStatus().equals(ColumnStatusEnum.field_grade.getType())) {
//            throw new BizException(ErrorCodeEnums.column_is_temporary_error);
//        }
        if (!pcColumnEntity.getDataType().equals(column.getDataType())) {
            List<PcRuleColumnEntity> ruleColumnEntityList = pcRuleColumnDao.selectByColumnId(column.getId());
            if (!CollectionUtils.isEmpty(ruleColumnEntityList)) {
                throw new BizException(ErrorCodeEnums.rule_column_has_column, "该字段已被规则使用，暂时无法修改");
            }
        }
        pcColumnEntity.setEventType(column.getEventType())
                .setEventIds(column.getEventIds())
                .setDisplayName(column.getDisplayName())
                .setDataType(column.getDataType())
                .setInfo(column.getInfo())
                .setModuleCode(column.getModuleCode());
        return pcColumnDao.updateByPrimaryKey(pcColumnEntity);
    }

    /**
     * 字段删除
     *
     * @param columnId
     * @return
     */
    @Override
    public Integer deleteColumn(Long columnId) {
        PcColumnEntity columnEntity = selectColumn(columnId);
        List<PcRuleColumnEntity> ruleColumnEntityList = pcRuleColumnDao.selectByColumnId(columnId);
        if (CollectionUtils.isNotEmpty(ruleColumnEntityList)) {
            throw new BizException(ErrorCodeEnums.rule_column_has_column);
        }
        if (ColumnStatusEnum.isFun(columnEntity.getStatus())) {
            //如果是函数  删除相关连字段    删除函数体
            functionColumnDao.updateDeletedByFunctionId(columnEntity.getId());
            functionContextDao.deleteByFunctionId(columnEntity.getId());
        }
        return pcColumnDao.deleteByPrimaryKey(columnId);
    }

    /**
     * 字段 查询
     *
     * @param merchantId
     * @param columnQueryInfo
     * @return
     */
    @Override
    public List<PcColumnEntity> listColumn(Long merchantId, ColumnQueryInfo columnQueryInfo, ApiPage apiPage) {
        ColumnMap columnMap = new ColumnMap();
        BeanUtils.copyProperties(columnQueryInfo, columnMap);
        columnMap.setMerchantId(merchantId);
        Page<PcColumnEntity> page = PageHelper.startPage(columnQueryInfo.getPageNum(), columnQueryInfo.getPageSize());
        List<PcColumnEntity> columnEntityList = pcColumnDao.selectColumnList(columnMap);
        apiPage.setTotal(page.getTotal());
        return columnEntityList;
    }


    public PcColumnEntity selectColumn(Long id) {
        PcColumnEntity pcColumnEntity = pcColumnDao.selectByPrimaryKey(id);
        if (null == pcColumnEntity) {
            throw new BizException(ErrorCodeEnums.column_is_not_exist_error);
        }
        return pcColumnEntity;
    }

    @Override
    public PcColumnEntity selectByMerchantAndCode(Long merchantId, String code) {
        return pcColumnDao.selectByMerchantIdAndColumnCode(merchantId, code);
    }

    @Override
    public List<PcRuleColumnEntity> findRuleColumnByRuleId(Long ruleId) {
        return pcRuleColumnDao.findByRuleId(ruleId);
    }

    @Override
    public List<PcColumnEntity> selectByMerchantIdAndEventId(Long merchantId,
                                                             List<Long> eventIds,
                                                             Integer columnType) {
        return pcColumnDao.selectByMerchantIdAndEventId(merchantId, eventIds, columnType);
    }

    @Override
    public PcColumnEntity findById(Long columnId) {
        return pcColumnDao.selectByPrimaryKey(columnId);
    }
}
